package com.chenxin.wms.process.simulate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsTaskSubType;
import com.chenxin.common.enums.wms.TaskStatus;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.*;
import com.chenxin.wms.domain.dto.ArriveSimulateParams;
import com.chenxin.wms.mapper.*;
import com.chenxin.wms.process.simulate.IProcArriveSimulate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProcArriveSimulate implements IProcArriveSimulate {
    @Autowired
    private WcsTaskMapper wcsTaskMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private StationMapper stationMapper;

    @Override
    public ProcResult call(ArriveSimulateParams params) {
        if (params == null) {
            return ProcResult.error("arrive_simulate_001", "请求参数不能为空");
        }
        // 获取参数
        String contNo = params.getContNo();
        if (StringUtils.isEmpty(contNo)) {
            return ProcResult.error("arrive_simulate_002", "托盘不能为空");
        }
        String stationNo = params.getStationNo();
        if (StringUtils.isEmpty(stationNo)) {
            return ProcResult.error("arrive_simulate_003", "目标站不能为空");
        }
        // 判断站台是否维护
        QueryWrapper<StationEntity> stationQW = new QueryWrapper<>();
        stationQW.eq("station_no", stationNo)
                .eq("is_active", "0").last("limit 1");
        StationEntity stationInfo = stationMapper.selectOne(stationQW);
        if (stationInfo == null) {
            return ProcResult.error("arrive_simulate_004", "目标站台【" + stationNo + "】未维护或不可用,请核查");
        }
        // 获取站台对应货位信息
        QueryWrapper<LocationEntity> locationQW = new QueryWrapper<>();
        locationQW.eq("station_no", stationNo).eq("is_active", "0").last("limit 1");
        LocationEntity locationInfo = locationMapper.selectOne(locationQW);
        if (locationInfo == null) {
            return ProcResult.error("arrive_simulate_005", "目标站台【" + stationNo + "】未维护可用货位信息,请核查");
        }
        // 站台货位
        String stationLc = locationInfo.getLocationNo();
        Long creator = params.getCreator();
        Date workDate = new Date();
        // 是否有相关设备任务
        QueryWrapper<WcsTaskEntity> wcsTaskQW = new QueryWrapper<>();
        wcsTaskQW.eq("cont_no", contNo).lt("trk_status", TaskStatus.DONE.getCode()).last("limit 1");
        WcsTaskEntity trkInfo = wcsTaskMapper.selectOne(wcsTaskQW);
        if (trkInfo == null) {
            return ProcResult.error("arrive_simulate_006", "托盘【" + contNo + "】未匹配到设备任务");
        }
        // 判断托盘库存是否已下架
        QueryWrapper<StockEntity> stockQW = new QueryWrapper<>();
        stockQW.eq("cont_no", contNo)
                .like("location_no", "HW")
                .last("limit 1");
        StockEntity stockInfo = stockMapper.selectOne(stockQW);
        if (stockInfo != null) {
            return ProcResult.error("arrive_simulate_005", "托盘【" + contNo + "】存在未下架的库存【" + stockInfo.getStockId() + "】");
        }
        // 匹配业务相关任务
        QueryWrapper<TaskEntity> taskQW = new QueryWrapper<>();
        taskQW.eq("from_cont_no", contNo)
                .eq("task_status", TaskStatus.EXEC.getCode());
        List<TaskEntity> taskList = taskMapper.selectList(taskQW);
        if (taskList == null || taskList.size() == 0) {
            return ProcResult.error("arrive_simulate_007", "托盘【" + contNo + "】无执行中的业务任务");
        }
        // 循环处理相关任务
        for (TaskEntity task : taskList) {
            Long taskId = task.getTaskId();
            String taskSubType = task.getTaskSubType();
            // 更新通用字段
            task.setCurPosition(stationLc);
            task.setStationNo(stationNo);
            task.setUpdater(creator);
            task.setUpdateDate(workDate);
            // 根据任务子类型来判断需要如何处理
            if (WmsTaskSubType.DELIVERY_OUT.getCode().equals(taskSubType)) {
                task.setTaskStatus(TaskStatus.DONE.getCode());
                task.setComments("模拟到达成功站台【" + stationNo + "】");
            } else if (WmsTaskSubType.EMPTY_OUT.getCode().equals(taskSubType)) {
                task.setTaskStatus(TaskStatus.DONE.getCode());
                stockMapper.deleteById(task.getStockId());
            }
        }
        taskMapper.updateById(taskList);
        // 循环处理对应库存
        QueryWrapper<StockEntity> contStockQW = new QueryWrapper<>();
        contStockQW.eq("cont_no", contNo);
        List<StockEntity> stockList = stockMapper.selectList(contStockQW);
        if (stockList != null && stockList.size() > 0) {
            for (StockEntity stock : stockList) {
                stock.setLocationNo(stationLc);
                stock.setUpdater(creator);
                stock.setUpdateDate(workDate);
            }
            stockMapper.updateById(stockList);
        }

        return ProcResult.success("模拟到达成功，托盘【" + contNo + "】至站台【" + stationNo + "】");
    }
}
